package cn.smbms.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import cn.smbms.dao.BillMapper;
import cn.smbms.dao.ProviderMapper;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.utils.MyBatisUtil;

public class BillService {
	public static final String PAGEPATH = "/WEB-INF/jsp/";

	public void getBillList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String productName = request.getParameter("productName");
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}
		Integer isPayment = null;
		try {
			isPayment = Integer.parseInt(request.getParameter("isPayment"));
		} catch (NumberFormatException e) {

		}
		List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getAllNameAndId();
		List<Bill> billList = sqlSession.getMapper(BillMapper.class).getBillListByCondition(productName, providerId,
				isPayment);
		MyBatisUtil.closeSqlSession(sqlSession);
		request.setAttribute("providerList", providerList);
		request.setAttribute("billList", billList);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productName", productName);
		map.put("providerId", providerId);
		map.put("isPayment", isPayment);
		request.setAttribute("cmap", map);
		request.getRequestDispatcher(PAGEPATH + "billList.jsp").forward(request, response);

	}

	public void getBillById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer productId = null;
		try {
			productId = Integer.parseInt(request.getParameter("productId"));
		} catch (NumberFormatException e) {
		}
		Bill bill = sqlSession.getMapper(BillMapper.class).getBillById(productId);
		List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getAllNameAndId();
		MyBatisUtil.closeSqlSession(sqlSession);
		request.setAttribute("bill", bill);
		request.setAttribute("providerList", providerList);
		String opr = request.getParameter("opr");
		request.getRequestDispatcher(PAGEPATH + "bill" + opr + ".jsp").forward(request, response);

	}

	public void addBill(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		String opr = request.getParameter("opr");
		if (opr.equals("show")) {
			List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getAllNameAndId();
			request.setAttribute("providerList", providerList);
			request.getRequestDispatcher(PAGEPATH + "billAdd.jsp").forward(request, response);
			return;
		} else if (opr.equals("add")) {
			String billCode = request.getParameter("billCode");
			String productName = request.getParameter("productName");
			String productUnit = request.getParameter("productUnit");
			Integer productCount = null;
			try {
				productCount = Integer.parseInt(request.getParameter("productCount"));
			} catch (NumberFormatException e) {
			}

			Double totalPrice = null;
			try {
				totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
			} catch (NumberFormatException e) {
			}
			Integer providerId = null;
			try {
				providerId = Integer.parseInt(request.getParameter("providerId"));
			} catch (NumberFormatException e) {
			}

			Integer isPayment = null;
			try {
				isPayment = Integer.parseInt(request.getParameter("isPayment"));
			} catch (NumberFormatException e) {
			}
			Bill b = new Bill(billCode, productName, productUnit, productCount, totalPrice, isPayment, providerId);
			int i = sqlSession.getMapper(BillMapper.class).addBill(b);
			MyBatisUtil.closeSqlSession(sqlSession);
			response.getWriter().print("<script>alert('账单添加" + (i == 1 ? "成功" : "失败")
					+ "！');location.href='bill-getBillList.do';</script>");

		}

	}

	public void updateBill(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer id = null;
		try {
			id = Integer.parseInt(request.getParameter("billId"));
		} catch (NumberFormatException e) {
		}
		String billCode = request.getParameter("billCode");
		String productName = request.getParameter("productName");
		String productUnit = request.getParameter("productUnit");
		Integer productCount = null;
		try {
			productCount = Integer.parseInt(request.getParameter("productCount"));
		} catch (NumberFormatException e) {
		}

		Double totalPrice = null;
		try {
			totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
		} catch (NumberFormatException e) {
		}
		Integer providerId = null;
		try {
			providerId = Integer.parseInt(request.getParameter("providerId"));
		} catch (NumberFormatException e) {
		}

		Integer isPayment = null;
		try {
			isPayment = Integer.parseInt(request.getParameter("isPayment"));
		} catch (NumberFormatException e) {
		}
		Bill b = new Bill(id, billCode, productName, productUnit, productCount, totalPrice, isPayment, providerId);
		System.out.println(b);
		int i = sqlSession.getMapper(BillMapper.class).updateBill(b);
		MyBatisUtil.closeSqlSession(sqlSession);

		response.getWriter().print(
				"<script>alert('账单修改" + (i == 1 ? "成功" : "失败") + "！');location.href='bill-getBillList.do';</script>");

	}

	public void deleteBill(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SqlSession sqlSession = MyBatisUtil.CreateSqlsession();
		Integer billId = null;
		try {
			billId = Integer.parseInt(request.getParameter("billId"));
		} catch (NumberFormatException e) {
		}
		int i = sqlSession.getMapper(BillMapper.class).deleteBill(billId);
		// int i = 1;
		MyBatisUtil.closeSqlSession(sqlSession);
		response.getWriter().print(
				"<script>alert('账单删除" + (i == 1 ? "成功" : "失败") + "！');location.href='bill-getBillList.do';</script>");

	}
}
